package com.mingqi.enetconnected.view.Dialog.time;

import android.app.Activity;
import android.app.Dialog;
import android.util.DisplayMetrics;
import android.view.Display;
import android.view.Gravity;
import android.view.View;
import android.view.WindowManager;
import android.widget.TextView;

import com.mingqi.enetconnected.R;

/**
 * Created by li on 2018/8/29.
 * WeChat 18571658038
 * author LiWei
 */
public class TimePairPickerManager {

    private Activity mActivity;
    private TextView tvCancel;
    private TextView tvConfirm;
    private TextView tvTitle;
    private TimePairPickView mTPPVLeft;
    private TimePairPickView mTPPVRight;
    private Dialog mDialog;
    private OnCancelClickListener mCancelListener;
    private onConfirmClickListener mConfirmListener;

    private int leftSelectedNum;
    private int rightSelectedNum;

    private String mLeftSelectedTime;
    private String mRightSelectedTime;

    public TimePairPickerManager(Activity activity) {
        mActivity = activity;
        initDialog(0.5);
    }

    public TimePairPickerManager(Activity activity, double height) {
        mActivity = activity;
        initDialog(height);
    }

    private void initDialog(double height) {

        mDialog = new Dialog(mActivity, R.style.my_bottom_dialog);
        mDialog.setContentView(mActivity.getLayoutInflater().inflate(R.layout.dialog_time_pair_picker, null));
        Display dd = mActivity.getWindowManager().getDefaultDisplay();
        DisplayMetrics dm = new DisplayMetrics();
        dd.getMetrics(dm);
        WindowManager.LayoutParams attributes = mDialog.getWindow().getAttributes();
        mDialog.getWindow().setGravity(Gravity.BOTTOM);
        attributes.height = (int) (dm.heightPixels * height);
        attributes.width = dm.widthPixels;

        tvCancel = mDialog.findViewById(R.id.tvCancel);
        tvConfirm = mDialog.findViewById(R.id.tvConfirm);
        tvTitle = mDialog.findViewById(R.id.tvTitle);
        mTPPVLeft = mDialog.findViewById(R.id.tppv_left);
        mTPPVRight = mDialog.findViewById(R.id.tppv_right);
        leftSelectedNum = mTPPVLeft.getCurrentPosition();
        rightSelectedNum = mTPPVRight.getCurrentPosition();

        mLeftSelectedTime = mTPPVLeft.getSelectTime();
        mRightSelectedTime = mTPPVRight.getSelectTime();

        setListener();
    }

    private void setListener() {

        mTPPVLeft.setOnSelectTimeListener(new TimePairPickView.OnSelectTimeListener() {
            @Override
            public void onSelected(int num) {
                leftSelectedNum = num;
                mLeftSelectedTime = mTPPVLeft.getSelectTime();
            }
        });

        mTPPVRight.setOnSelectTimeListener(new TimePairPickView.OnSelectTimeListener() {
            @Override
            public void onSelected(int num) {
                rightSelectedNum = num;
                mRightSelectedTime = mTPPVRight.getSelectTime();
            }
        });

        tvConfirm.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                if (mConfirmListener != null) {

                    mConfirmListener.onClick(leftSelectedNum, rightSelectedNum, mLeftSelectedTime, mRightSelectedTime);
                }
            }
        });

        tvCancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mCancelListener != null) {
                    mCancelListener.onClick();
                }
                dismiss();
            }
        });
    }

    public void show() {
        if (mDialog != null) {
            mDialog.show();
        }
    }

    public void dismiss() {
        if (mDialog != null) {
            mDialog.cancel();
        }
    }

    public void selectNum(int leftNum, int rightNum) {
        mTPPVLeft.select(leftNum);
        mTPPVRight.select(rightNum);
    }

    public void setOnCancelListener(OnCancelClickListener listener) {
        this.mCancelListener = listener;
    }

    public void setOnConfirmListener(onConfirmClickListener listener) {
        this.mConfirmListener = listener;
    }

    public void setTitle(String title) {
        tvTitle.setText(title);
    }

    public interface OnCancelClickListener {
        void onClick();
    }

    public interface onConfirmClickListener {

        void onClick(int leftNum, int rightNum, String leftTime, String rightTime);
    }
}
